package com.process;

import com.example.model.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * Created with IntelliJ IDEA.
 * ClassName: KeyByProcessDemo
 * Package: com.process
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-25
 * Time: 16:29
 */

//按键分区处理函数
public class KeyByProcessDemo {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> data = env.socketTextStream("hadoop102", 7777)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        final String[] s = value.split(" ");
                        return new WaterSensor(s[0], Long.valueOf(s[1]), Integer.valueOf(s[2]));
                    }
                }).assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                //设置升序
                                .<WaterSensor>forMonotonousTimestamps()
                                //从数据中提取水位线
                                .withTimestampAssigner((val, ts) -> val.getTs() * 1000L)
                );

        data.keyBy(value -> value.getId())
                //调用process算子
                //创建KeyedProcessFunction 参数分别是 key的类型 输入数据的类型 输出类型
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {
                    @Override
                    public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                        /**
                         * 这个方法是来一条数据 执行一次 就会创建一个定时器
                         * 而且触发条件是 watermark >= ts 此时 1毫秒要考虑进去
                         * 所以5秒的闹钟 6秒才会触发
                         * 而且闹钟只触发了一次 会自动去重同一个key的闹钟
                         */
                        //数据中提取出来的事件时间 如果没有则为null
                        Long timestamp = ctx.timestamp(); //这个时间来源于 数据提取器
                        //创建定时器 时间到了 执行onTime方法
                        TimerService timerService = ctx.timerService();
                        //注册事件时间的定时器
                        timerService.registerEventTimeTimer(5000L);
                        System.out.println("当前时间timestamp=" + timestamp + "注册一个5秒定时器");


                        //初始化 处理时间定时器
                        //timerService.registerProcessingTimeTimer();

                        //删除事件时间
                        //timerService.deleteEventTimeTimer();

                        //都是时间戳
                        System.out.println("当前的处理时间" + timerService.currentProcessingTime());
                        System.out.println("waterMark事件时间" + timerService.currentWatermark());
                    }

                    @Override
                    public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                        //闹钟响的触发
                        System.out.println("现在时间是" + timestamp + "定时器触发了");
                    }
                }).print();


        env.execute();

    }
}
